package com.xinnet.core.validate;

import java.util.Properties;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

import com.xinnet.core.validate.attribute.DoublePrecision;


/**
 * 正浮点数或非负整数用的Validator类。
 * 
 * @author wenjianye 
 * 请输入正浮点数或非负整数。
 */
public class DoublePrecisionValidator  extends Validator  implements
		ConstraintValidator<DoublePrecision, Object> {

	private static final String REGEX_PATTERN_DOUBLE_POSITIVE_OR_INT = "^(([0-9]+\\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\\.[0-9]+)|([0-9]*[1-9][0-9]*))$|^\\d+$";
	private static Properties prop;
	@Override
	public void initialize(DoublePrecision arg0) {

	}
	/**
	 * 接口的验证方法
	 * */
	@Override
	public boolean isValid(Object value,
			ConstraintValidatorContext constraintContext) {

		return super.isValid(value,constraintContext);
	}

	public boolean valid(Object value) {
		if (value == null || value instanceof Number) {
			return true;
		}

		return ValidatorUtil.patternMatches(
				REGEX_PATTERN_DOUBLE_POSITIVE_OR_INT, value);
	}

	/**
	 * 未通过验证时,提示错误信息
	 * */
	public String getMessage() {
		this.getClass().getCanonicalName();
		// "请输入正浮点数或非负整数。";
		return prop
				.getProperty(this.getClass().getName());
	}

	

}
